import 'package:get/get.dart';
import 'package:safetymonitor/app/components/toast_widget.dart';
import 'package:safetymonitor/app/core/http/http.dart';
import 'package:safetymonitor/app/core/utils/interface.dart';
import 'package:safetymonitor/app/core/utils/user_manager.dart';
import 'package:safetymonitor/app/data/entities/application/department_user_entity.dart';
import 'package:safetymonitor/app/data/entities/application/preventive_rectification_get_entity.dart';
import 'package:safetymonitor/modules/home/snap_shot_danger/no_completed/controller.dart';
import 'package:safetymonitor/router/app_pages.dart';
///待派发
class NoDistributedStateController extends GetxController {
  /// 当前选中的标题索引，0：基本信息 1:隐患整改
  RxInt currentIndex = 0.obs;
  PreventiveRectificationGetEntity preventiveRectificationGetEntity=PreventiveRectificationGetEntity();

  ///	隐患上报视频集合[数组]
  List<String>? dangerVideo = [];
  List<String>? dangerImages = [];
  @override
  void onInit() {
    super.onInit();
    initData();
  }
  /// 初始化数据
  Future<void> initData() async {
    //获取组织数据
    final res = await HttpUtil().generateGetRequest<PreventiveRectificationGetEntity>(
        Interface.preventiveRectificationGet,
        queryParameters: {
          'id': Get.arguments
        }).execute(isShowLoading: true);
    res.onSuccess((result) {
      if (result.data != null) {
        preventiveRectificationGetEntity= result.data ??PreventiveRectificationGetEntity();
      }
      update();
    });
  }
  /// title选择切换
  void selectTitle(int index) {
    currentIndex.value = index;
  }

  /// 选择隐患整改人
  void selectReportingPersonnel() {
    //第一种方式
    Get.toNamed(Routes.personnelDepartmentTreePage, arguments: {
      // 'queryParameters': widget.queryParameters,
      'title':UserManager.getUserCompanyName(),
      'isRadio': false,
    })?.then((value) {
      //如果不为空则设置人员信息
      if (value != null) {
        final List<DepartmentUserEntity> personList = value as List<DepartmentUserEntity>;
        // preventiveRectificationGetEntity.rectificationUserIds = personList[0].id;
        // preventiveRectificationGetEntity.rectificationUserNames =personList[0].getShowName();
        preventiveRectificationGetEntity.rectificationUserIds =  personList.map((e) => e.id).toList().join(',');
        preventiveRectificationGetEntity.rectificationUserNames =  personList.map((e) => e.getShowName()).toList().join(',');
        update();
      }
    });
    // Get.toNamed(Routes.selectPeople)?.then((value) {
    //   if (value != null && value is UserRespEntity) {
    //     final UserRespEntity confirmBy = value;
    //     preventiveRectificationGetEntity.rectificationPersonId = confirmBy.id;
    //     preventiveRectificationGetEntity.rectificationPersonName =confirmBy.getShowName();
    //     update();
    //   }
    // });
  }

  /// 提交
  void submit() async {
    //判断验收 和描述
    if(  preventiveRectificationGetEntity.rectificationUserNames!.trim().isEmpty){
      AppToast.showWarning('请选择整改人');
      return;
    }
    final res = await HttpUtil()
        .generatePutRequest(
      Interface.preventiveRectificationDispatch,
      data: preventiveRectificationGetEntity.toJson(),
    )
        .execute(isShowLoading: true);
    res.onSuccess((result) {
      Get.back(result: true);
      AppToast.showWarning('提交成功');
      Get.find<NoCompletedController>().globalKey.currentState?.onRefresh();
    });
  }
}


